package com.midea.ai.air.ui.home;

import android.animation.ValueAnimator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.common.FragmentTabManager;
import com.midea.ai.air.ui.device.DeviceFragmemt;
import com.midea.ai.air.ui.more.MoreFragmemt;
import com.midea.ai.air.ui.user.LoginActivity;
import com.midea.ai.air.utils.FastBlur;

import java.util.ArrayList;
import java.util.List;

public class MainActivityNew extends BaseActivity {
    private RadioGroup mRadioGroup;
    private RadioButton mHomeRadio;
    private RadioButton mDeviceRadio;
    private RadioButton mMoreRadio;
    private RadioButton mVoiceRadio;
    private FragmentTabManager mFragmentTabManager;
    public List<Fragment> mFragments = new ArrayList<Fragment>();

    private View mViewHomeSet;
    private RelativeLayout page_Rl;
    private FrameLayout page_Fl;

    public static final String SCAN_THE_QR_CODE_TAG = "SCAN_THE_QR_CODE";
    public final static String SCANNING_REQUEST_CODE_RESULT = "SCANNING_RESULT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isLogin()) {
            navigate(LoginActivity.class);
        }
        setContentView(R.layout.activity_main_new);
        findViewById(R.id.radio_group_tabs).getRootView().setBackgroundResource(R.color.pager_center_bg_color);
        initViews();
    }

    /**
     * 初始化View
     */
    private void initViews() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radio_group_tabs);
        int width = getResources().getDimensionPixelOffset(R.dimen.height_20dp);
        int height = getResources().getDimensionPixelOffset(R.dimen.height_20dp);
        mHomeRadio = (RadioButton) findViewById(R.id.xcRadio);
        setDrawableTop(mHomeRadio, R.drawable.btn_home_selector, width, height);

        mDeviceRadio = (RadioButton) findViewById(R.id.yrRadio);
        setDrawableTop(mDeviceRadio, R.drawable.btn_device_selector, width, height);

        mMoreRadio = (RadioButton) findViewById(R.id.fxRadio);
        setDrawableTop(mMoreRadio, R.drawable.btn_more_selector, width, height);

        mVoiceRadio = (RadioButton) findViewById(R.id.meRadio);
        setDrawableTop(mVoiceRadio, R.drawable.btn_home_selector, width, height);

        mFragments.add(new HomeFragmemt());
        mFragments.add(new DeviceFragmemt());
        mFragments.add(new MoreFragmemt());
//        mFragments.add(new ChatFragment());

        mFragmentTabManager = new FragmentTabManager(this, mFragments, R.id.lay_content, mRadioGroup);

        mViewHomeSet = findViewById(R.id.homePageSettingFragment_framelayout);
        mViewHomeSet.setTranslationY(Constant.sRealHeight);

        page_Rl = (RelativeLayout) findViewById(R.id.main_new_page_rl);
        page_Fl = (FrameLayout) findViewById(R.id.homePageSettingFragment_framelayout);
    }

    /**
     * 设置图标
     *
     * @param radioButton
     * @param drawableId
     * @param width
     * @param height
     */
    private void setDrawableTop(RadioButton radioButton, int drawableId, int width, int height) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, width, height);
        radioButton.setCompoundDrawables(null, drawable, null, null);
    }

    /**
     * 打开或者关闭主页定制页面
     */
    public void startOrStopPageSet(boolean isStart) {
        ValueAnimator animator;
        if (isStart) {
            animator = ValueAnimator.ofFloat(Constant.sRealHeight, 0);
//            initPageSet();

        } else {
            bitmap = null;
            animator = ValueAnimator.ofFloat(0, Constant.sRealHeight);
        }
        animator.setDuration(500).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mViewHomeSet.setTranslationY((float) animation.getAnimatedValue());
            }
        });
    }

    private Bitmap bitmap = null;

    private void initPageSet() {
        page_Rl.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                page_Rl.getViewTreeObserver().removeOnPreDrawListener(this);
                page_Rl.buildDrawingCache();

                bitmap = page_Rl.getDrawingCache();
                Log.e("hwwlog", bitmap + "");
                blur(bitmap, page_Fl);
                return true;
            }
        });

    }

    private void blur(Bitmap bkg, View view) {
        long startMs = System.currentTimeMillis();
        float scaleFactor = 1;
        float radius = 20;
        Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()), (int) (view.getMeasuredHeight()),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        canvas.translate(-view.getLeft(), -view.getTop());
        canvas.scale(1 / scaleFactor, 1 / scaleFactor);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bkg, 0, 0, paint);
        overlay = FastBlur.doBlur(overlay, (int) radius, true);
        view.setBackgroundDrawable(new BitmapDrawable(getResources(), overlay));
        Log.e("hwwlog", System.currentTimeMillis() - startMs + "ms");
    }

}
